﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NPinyin;
using System.Text.RegularExpressions;
using System.Text;
using System;
using System.Linq;

namespace VIP.Extension
{

    public static class VIPStringExtension
    {

        /// <summary>
        /// 字符串是否为空
        /// </summary>
        /// <param name="selfStr">需要判断的字符串</param>
        /// <returns>是否</returns>
        public static bool IsEmpty(this string selfStr)
        {
            if (string.IsNullOrEmpty(selfStr))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 字符串是否不为空
        /// </summary>
        /// <param name="selfStr">需要判断的字符串</param>
        /// <returns>是否</returns>
        public static bool IsNotEmpty(this string selfStr)
        {
            if (!string.IsNullOrEmpty(selfStr))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 中文转拼音
        /// </summary>
        /// <param name="selfStr">需要转换的字符串</param>
        /// <returns>转换后的字符串</returns>
        public static string ToPinyin(this string selfStr)
        {
            return Pinyin.GetPinyin(selfStr);
        }

        /// <summary>
        /// 中文转拼音首字母
        /// </summary>
        /// <param name="selfStr">需要转换的字符串</param>
        /// <returns>转换后的字符串</returns>
        public static string ToInitials(this string selfStr)
        {
            return Pinyin.GetInitials(selfStr);
        }

        /// <summary>
        /// 将字符串进行MD5加密
        /// </summary>
        /// <param name="selfStr">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string MD5(this string selfStr)
        {
            //加密后转换成大写
            string encryptStr = VIPEncryptUtil.EncryptMD5(selfStr);
            return encryptStr;
        }

        /// <summary>
        /// 将字符串进行DES加密
        /// </summary>
        /// <param name="selfStr">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string DES(this string selfStr)
        {
            string encryptStr = VIPEncryptUtil.EncryptDES(selfStr);
            return encryptStr;
        }

        /// <summary>
        /// 将字符串进行DES解密
        /// </summary>
        /// <param name="selfStr"></param>
        /// <returns></returns>
        public static string UnDES(this string selfStr)
        {
            string decryptStr = VIPEncryptUtil.DecryptDES(selfStr);
            return decryptStr;
        }

        /// <summary>
        /// 将特殊字符编码(例如：中文)(测试仅支持UTF-8格式的文本文件)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Escape(this string str)
        {
            string newStr = Regex.Escape(str);
            return newStr;
        }

        /// <summary>
        /// 解码特殊字符(例如：中文)(测试仅支持UTF-8格式的文本文件)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Unescape(this string str)
        {
            string newStr = Regex.Unescape(str);
            return newStr;
        }

        /// <summary>
        /// 在数据的首部加上数据的长度(占4个字节)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] AddHeader(this string data)
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(data);
            int dataLength = dataBytes.Length;
            byte[] lengthBytes = BitConverter.GetBytes(dataLength);
            byte[] newBytes = lengthBytes.Concat(dataBytes).ToArray();
            return newBytes;
        }

    }

}